﻿using BalanceControl.Comm;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Geared;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BalanceControl
{
    /// <summary>
    /// PageScope.xaml 的交互逻辑
    /// </summary>
    public partial class PageScope : Page
    {
        public SeriesCollection LastHourSeries { get; set; }

        public PageScope()
        {
            InitializeComponent();
            LastHourSeries = new SeriesCollection
            {
                //四个通道
                new LineSeries
                {
                    //AreaLimit = -10,
                    Values = new GearedValues<ObservableValue>{}
                },
                new LineSeries
                {
                    //AreaLimit = -10,
                    Values = new GearedValues<ObservableValue>{}
                },
                new LineSeries
                {
                    //AreaLimit = -10,
                    Values = new GearedValues<ObservableValue>{}
                },
                new LineSeries
                {
                    //AreaLimit = -10,
                    Values = new GearedValues<ObservableValue>{}
                }
            };
            SerialHandler = new SerialHandler();
            TcpHandler = new TcpHandler();

            SerialHandler.FrameReceived = new Action<byte[]>((data) =>
            {
                append_data(data[2], BitConverter.ToSingle(data, 3));

            });

            TcpHandler.FrameReceived = new Action<byte[]>((data) =>
            {
                append_data(data[2], BitConverter.ToSingle(data, 3));

            });

            DataContext = this;

            LineSeries a = new LineSeries();
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            cbSerialPort.Items.Clear();
            foreach (string name in SerialPort.GetPortNames())
            {
                cbSerialPort.Items.Add(name);
            }
        }

        private void btnSerialOpen_Click(object sender, RoutedEventArgs e)
        {
            SerialHandler serialHandler = CommManager.GetInstance().GetSerialHandler();
            //串口已打开
            if (serialHandler.IsConnect)
            {
                serialHandler.DisConnect();
                btnSerialOpen.Content = "打开";
                return;
            }

            if (cbSerialPort.SelectedItem == null || cbSerialPort.SelectedItem.ToString() == "")
            {
                MessageBox.Show("请选择正确的端口号！");
                return;
            }
            int band = 0;
            if (cbSerialBaud.SelectedItem == null || cbSerialBaud.SelectedItem.ToString() == "")
            {
                MessageBox.Show("请选择正确的波特率！");
                return;
            }
            try
            {
                if (serialHandler.Connect(cbSerialPort.Text, int.Parse(cbSerialBaud.Text)))
                {
                    //打开串口成功
                    btnSerialOpen.Content = "关闭";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败");
                Console.WriteLine(ex.StackTrace);
            }
        }

        private void btnSocketOpen_Click(object sender, RoutedEventArgs e)
        {
            string host = txtSocketAddress.Text;
            int port = int.Parse(txtSocketPort.Text);
            CommManager.GetInstance().GetTcpHandler().Connect(host, port);
        }

        private void append_data(int channel, float value)
        {
            if (channel >= 4) return;
            LastHourSeries[channel].Values.Add(new ObservableValue(value));
            if (LastHourSeries[channel].Values.Count > 500)
            {
                LastHourSeries[channel].Values.RemoveAt(0);
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (checkSerialCH1.IsChecked == true)
            {
                (LastHourSeries[0] as LineSeries).Visibility = Visibility.Visible;
            }
            else
            {
                (LastHourSeries[0] as LineSeries).Visibility = Visibility.Hidden;
            }
            if (checkSerialCH2.IsChecked == true)
            {
                (LastHourSeries[1] as LineSeries).Visibility = Visibility.Visible;
            }
            else
            {
                (LastHourSeries[1] as LineSeries).Visibility = Visibility.Hidden;
            }
            if (checkSerialCH3.IsChecked == true)
            {
                (LastHourSeries[2] as LineSeries).Visibility = Visibility.Visible;
            }
            else
            {
                (LastHourSeries[2] as LineSeries).Visibility = Visibility.Hidden;
            }
            if (checkSerialCH4.IsChecked == true)
            {
                (LastHourSeries[3] as LineSeries).Visibility = Visibility.Visible;
            }
            else
            {
                (LastHourSeries[3] as LineSeries).Visibility = Visibility.Hidden;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string senderName = (sender as Button).Name;
            switch (senderName)
            {
                case "btnP":
                    //帧头 主功能码 次功能码 数据
                    SerialHandler.Transmit(new byte[] { 0x55, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 });
                    break;
                case "btnChannel":
                    byte channel = byte.Parse(txtChannel.Text);
                    float value = float.Parse(txtChannelValue.Text);
                    TcpHandler.Transmit(new ScopeFrame(0x02, channel, value).GetBytes());
                    break;
            }
        }


    }
}
